/*
Copyright [2020] [https://www.stylefeng.cn]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Guns采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点，可申请商业授权，获取Guns商业授权许可，请在官网购买授权，地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.exception;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.consts.CommonConstant;
import cn.stylefeng.guns.core.exception.enums.AuthExceptionEnum;
import cn.stylefeng.guns.core.exception.enums.abs.AbstractBaseExceptionEnum;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 认证相关的异常
 * <p>
 * 认证和鉴权的区别：
 * <p>
 * 认证可以证明你能登录系统，认证的过程是校验token的过程
 * 鉴权可以证明你有系统的哪些权限，鉴权的过程是校验角色是否包含某些接口的权限
 *
 * @author xuyuxiang
 * @date 2020/3/12 9:55
 */
@Getter
public class AuthException extends RuntimeException {

    private final Integer code;

    private final String errorMessage;

    public AuthException(AbstractBaseExceptionEnum exception) {
        super(exception.getMessage());
        this.code = exception.getCode();
        this.errorMessage = exception.getMessage();
    }

    public AuthException(String key, String locale) {
        Map<String, String> map = new HashMap<>();
        map.put("account_pwd_empty_zh", "用户名或密码为空");
        map.put("account_pwd_empty_en", "User name or password is empty");
        map.put("account_pwd_error_zh", "用户名或密码错误，失败" + CommonConstant.MAX_LOGIN_FAILED_LOCK_COUNT + "次后会被锁定，"+ CommonConstant.USER_LOCKED_TIME_MIN +"分钟后解锁");
        map.put("account_pwd_error_en", "Incorrect username or password, locked after "+ CommonConstant.MAX_LOGIN_FAILED_LOCK_COUNT +" failures, unlocked after "+ CommonConstant.USER_LOCKED_TIME_MIN +" minutes");
        map.put("valid_code_error_zh", "验证码错误，请检查captcha参数");
        map.put("valid_code_error_en", "Verification code error, please check captcha parameters");
        map.put("request_token_empty_zh", "请求token为空，请携带token访问本接口");
        map.put("request_token_empty_en", "Request token is empty, please bring the token to access this interface");
        map.put("not_valid_token_type_zh", "token格式不正确，token请以Bearer开头，并且Bearer后边带一个空格");
        map.put("not_valid_token_type_en", "The token format is incorrect. Please start with Bearer and have a space after Bearer");
        map.put("request_token_error_zh", "请求token错误");
        map.put("request_token_error_en", "Request token error");
        map.put("account_freeze_error_zh", "账号被冻结，请联系管理员");
        map.put("account_freeze_error_en", "Account frozen, please contact administrator");
        map.put("login_expired_zh", "登录已过期，请重新登录");
        map.put("login_expired_en", "Login has expired, please log in again");
        map.put("no_login_user_zh", "无登录用户");
        map.put("no_login_user_en", "No logged in user");
        map.put("user_locked_zh", "用户名或密码错误，失败5次后会被锁定，5分钟后解锁");
        map.put("user_locked_en", "Incorrect username or password, locked after "+ CommonConstant.MAX_LOGIN_FAILED_LOCK_COUNT +" failures, unlocked after "+ CommonConstant.USER_LOCKED_TIME_MIN +" minutes");
        map.put("captcha_null_zh", "验证码为空,请输入验证码");
        map.put("captcha_null_en", "The verification code is empty, please enter the verification code");
        map.put("captcha_exp_time_zh", "验证已失效,请重新刷新验证码");
        map.put("captcha_exp_time_en", "Verification has expired, please refresh the verification code again");
        map.put("captcha_validate_error_zh", "验证码错误");
        map.put("captcha_validate_error_en", "Verification code error");
        map.put("decrypt_error_zh", "用户名密码解密错误");
        map.put("decrypt_error_en", "User name and password decryption error");
        map.put("account_cert_inconsistent_zh", "登录账号和证书信息不一致");
        map.put("account_cert_inconsistent_en", "Inconsistent login account and certificate information");
        map.put("account_cert_fail_zh", "免密登录失败");
        map.put("account_cert_fail_en", "Password free login failed");
        map.put("invalid_reuqest_zh", "请求无效");
        map.put("invalid_reuqest_en", "Invalid request");
        if(StrUtil.isBlank(locale)) {
            locale = "zh";
        }

        this.code = 0;
        String message = map.get(key + "_" + locale);
        this.errorMessage = message;
    }

}
